﻿<html>
  <header>
    <!-- <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> -->
    <script src="./jweixin-1.6.0.js"></script>

    <style>
      .btn-outer {
        width: 100px;
        height: 100px;
        border: #ccc 1px solid;
      }
      #launch-btn {
        background: #ff4433;
        border-radius: 4px;
      }
    </style>

    <script>
      // 设置签名
      function setConfig(params) {
        wx.config({
          // debug: true,
          appId: "wxec3dfc469a3b0a69", // 必须和当前网页的公众号一致
          timestamp: params.timestamp, // 后端生成
          nonceStr: params.nonceStr, // 后端生成
          signature: params.signature, // 后端生成
          // timestamp: "1761190301",
          // nonceStr: "733b852b-dbcb-44ad-a3f5-e43d7528d70f",
          // signature: "d68e0a5608267c9eeb5598b712cbbdc5a25f6d3b",
          jsApiList: [
            "invokeMiniProgramAPI",
            "navigateToMiniProgram",
            // "launchMiniProgram",
          ], // 必须声明该接口,
          openTagList: ["wx-open-launch-weapp"], // 必须添加这一行
        });
      }

      // 按钮1，根据search设置path属性
      function setPath(params) {
        let searchStr = location.search;
        const tmpEl = document.getElementById("launch-btn");
        tmpEl.setAttribute("path", "pages/index/open2" + searchStr);
      }

      // 按钮2，根据search获取schema
      function getSchema() {
        let searchStr = location.search;
        if (searchStr.startsWith("?")) {
          searchStr = searchStr.substring(1);
        }
        return fetch("https://zlyz.jdsn.cn/t/sysOperationLog/getSchema", {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            jump_wxa: {
              path: "/pages/index/open2",
              query: searchStr,
              // release trial develop
              // env_version: "release",
            },
          }),
        });
      }

      // 按钮1，根据href获取签名
      function getConfig() {
        const getSignUrl =
          "https://zlyz.jdsn.cn/t/sysOperationLog/getSign?url=" + location.href;
        fetch(getSignUrl).then((res) => {
          const bbb = res.json().then((json) => {
            if (json.success) {
              setConfig(json.result);
            }
          });
        });
      }
      // 页面加载完成后尝试跳转
      window.onload = function () {
        // 给第一个按钮用
        getConfig();
        // setConfig();
        setPath();
        // 给第二个按钮用
        getSchema().then((res) => {
          const bbb = res.json().then((json) => {
            const weappScheme = json.result.openlink;
            // const weappScheme = "weixin://dl/business/?t=YvjJ8XzopSj";
            const tmpEl = document.getElementById("gotoBtn");
            tmpEl.addEventListener("click", function (params) {
              window.location.href = weappScheme;
            });
          });
        });
      };

      // 验证是否初始化成功
      wx.ready(function () {
        console.log("JS-SDK 初始化成功！");
      });

      wx.error(function (err) {
        console.error("JS-SDK 初始化失败:", err);
      });
    </script>
  </header>
  <body>
    <div
      style="
        display: flex;
        justify-content: center;
        padding: 12px;
        border: #ccc 1px solid;
        margin-bottom: 12px;
      "
    >
      <wx-open-launch-weapp
        id="launch-btn"
        appid="wx2ce68e029e5f4b5f"
        username="gh_26e8fea4c5d3"
        path="pages/index/open2"
      >
        <script type="text/wxtag-template">
          <style>.btn { padding: 12px }</style>
          <style>.btn-outer { color: #ffccee }</style>
          <div class="btn-outer">
            <button class="btn">即将打开小程序</button>
          </div>
        </script>
      </wx-open-launch-weapp>
    </div>
    <div
      style="
        display: flex;
        justify-content: center;
        padding: 12px;
        border: #ccc 1px solid;
      "
    >
      <div style="padding: 4px; height: 40px">
        <button id="gotoBtn" style="height: 40px">即将打开小程序</button>
      </div>
    </div>
  </body>
</html>
